package com.astrob.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.model.SpecialTypeBean;
import com.astrob.nhcmap.R;

import java.util.ArrayList;
import java.util.List;

public class ViewPager_Specail_ItemAdapter extends BaseAdapter {

	private List<SpecialTypeBean> list_info;
    private Context context;
    /** ViewPager页码 */  
    private int index;  
    /** 根据屏幕大小计算得到的每页item个数 */  
    private int pageItemCount;  
    /** 传进来的List的总长度 */  
    private int totalSize;  
  
    /** 当前页item的实际个数 */  
    // private int itemRealNum;  
    @SuppressWarnings("unchecked")
    public ViewPager_Specail_ItemAdapter(Context context, List<?> list) {
        this.context = context;  
        this.list_info = (List<SpecialTypeBean>) list;
    }  
  
    public ViewPager_Specail_ItemAdapter(Context context, List<?> list, int index, int pageItemCount) {
        this.context = context;  
        this.index = index;  
        this.pageItemCount = pageItemCount;  
        list_info = new ArrayList<SpecialTypeBean>();
        totalSize = list.size();  
        // itemRealNum=list.size()-index*pageItemCount;  
        // 当前页的item对应的实体在List<?>中的其实下标  
        int list_index = index * pageItemCount;  
        for (int i = list_index; i < list.size(); i++) {  
            list_info.add((SpecialTypeBean) list.get(i));
        }  
  
    }  
  
    @Override
    public int getCount() {  
        int size = totalSize / pageItemCount;  
        if (index == size)  
            return totalSize - pageItemCount * index;  
        else  
            return pageItemCount;  
        // return itemRealNum;  
    }  
  
    @Override
    public Object getItem(int arg0) {
        return null;  
    }  
  
    @Override
    public long getItemId(int position) {  
        return position;  
    }  
  
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder iv;  
        if (convertView == null){
        	convertView = LinearLayout.inflate(context,
					R.layout.btn_specialtype, null);
        	iv = new ViewHolder();
			iv.img = (TextView) convertView
					.findViewById(R.id.radio_mstype);
			convertView.setTag(iv);
        }
              
        else{
            iv = (ViewHolder) convertView.getTag();        	
        }
        boolean selected = list_info.get(position).isSelected();
        //iv.img.setBackgroundResource(list_info.get(position).getResId());
        Drawable drawable= context.getResources().getDrawable(list_info.get(position).getResId());
	    /// 这一步必须要做,否则不会显示.
        //Drawable drawbottom =
        View view = new View(context);
        if(selected){
        	view.setBackgroundColor(list_info.get(position).getSelectedClr());
        }
        else{
        	view.setBackgroundColor(Color.WHITE);
        }
        Drawable drawbottom = view.getBackground();
	    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
	    drawbottom.setBounds(0, 0, (int)(drawable.getMinimumWidth()*1.5), dip2px(context, 3));
	    if(Build.VERSION.SDK_INT > 8){
	    	iv.img.setCompoundDrawables(null,drawable,null,drawbottom);
	    } else{
	    	iv.img.setCompoundDrawables(null,drawable,null,null);
	    	if(selected){
	    		iv.img.setTextColor(list_info.get(position).getSelectedClr());
	        }
	        else{
	        	iv.img.setTextColor(0xff9c9c9c);
	        }
	    }
	      
        iv.img.setText(list_info.get(position).getStrText());
        
        if(selected){
        	iv.img.setEnabled(false);
        	//iv.img.setTextColor(R.color.checkbox_d_text_color);
        }
        else{
        	//iv.img.setTextColor(R.color.checkbox_n_text_color);
        	iv.img.setEnabled(true);
        }
        return convertView;
    }  
  
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  
    class ViewHolder {  
    	TextView img;
    }  

}
